﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace Data
{
    public class NewsData : DataBase
    {

        public void DeleteNews(int id)
        {
            var parametrs = new List<SqlParameter>
                                {
                                    new SqlParameter("@Id", id)
                                };

            ExecuteNonQuery("DeleteNews", parametrs);
        }

        public void UpdateNews(News news)
        {
            var parametrs = new List<SqlParameter>
                                {
                                    new SqlParameter("@Id", news.Id),
                                    new SqlParameter("@Title", news.Title),
                                    new SqlParameter("@Text", news.Text),
                                    new SqlParameter("@Author", news.Author),
                                    new SqlParameter("@PublishDate", news.PublishDate),
                                    new SqlParameter("@UpdateDate", news.UpdateDate),
                                    new SqlParameter("@Category", news.Category),
                                    new SqlParameter("@Rating", news.Rating)
                                };
            ExecuteNonQuery("UpdateNews", parametrs);
        }

        public void AddTagsBind(int tagId, int newsId)
        {
            var parametrs = new List<SqlParameter>
                                {
                                    new SqlParameter("@TagId", tagId),
                                    new SqlParameter("@NewsId", newsId)
                                };
            ExecuteNonQuery("AddTagsBind", parametrs);
        }


        public List<News> GetNews(int id = 0, int category = 0, string title = null, string author = null,
                                  string text = null, string tagName = null, string orderBy = null,
                                    int? publishDate = null)
        {
            var parametrs = new List<SqlParameter>
                                {
                                    new SqlParameter("@Id", id),
                                    new SqlParameter("@Category", category),
                                    new SqlParameter("@Title", title),
                                    new SqlParameter("@Author", author),
                                    new SqlParameter("@Text", text),
                                    new SqlParameter("@TagName", tagName),
                                    new SqlParameter("@OrderBy", orderBy),
                                    new SqlParameter("@PublishDate", publishDate)
                                };

            var dataTable = ExecuteDataTable("GetNews", parametrs);
            if (dataTable.Rows.Count > 0)
            {
                var newsList = new List<News>();
                var dbImages = new ImagesData();
                foreach (DataRow row in dataTable.Rows)
                {
                    var news = new News
                                   {
                                       Id = Convert.ToInt32(row["ID"]),
                                       Title = Convert.ToString(row["Title"]),
                                       Category = Convert.ToInt32(row["Category"]),
                                       Author = Convert.ToString(row["Author"]),
                                       Text = Convert.ToString(row["Text"]),
                                       Images = dbImages.GetNewsImagesUrl(Convert.ToInt32(row["Id"])),
                                       PublishDate = CheckDbNull(row["PublishDate"]) as DateTime?,
                                       UpdateDate = CheckDbNull(row["UpdateDate"]) as DateTime?,
                                       Rating = GetRating(Convert.ToInt32(row["ID"]))
                                       
                                   };
                    if (id != 0)
                    {
                        news.TagsList = GetTagsInNews(news.Id);
                    }
                    newsList.Add(news);
                }
                return newsList;
            }
            return null;
        }

        private List<Tag> GetTagsInNews(int id)
        {
            var parametrs = new List<SqlParameter> {new SqlParameter("@NewsId", id)};
            var dataTable = ExecuteDataTable("GetTagsInNews", parametrs);
            var tagsList = new List<Tag>();
            if (dataTable.Rows.Count > 0)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    tagsList.Add(new Tag
                                     {
                                         Id = Convert.ToInt32(row["TagID"]),
                                         Name = Convert.ToString(row["Name"])
                                     });
                }
                return tagsList;
            }
            return tagsList;
        }

        public void PublishNews(int id)
        {
            var parameters = new List<SqlParameter>
                                 {
                                     new SqlParameter("@Id", id)
                                 };
            ExecuteNonQuery("PublishNews", parameters);
        }

        public int AddNews(News news)
        {
            var parametrs = new List<SqlParameter>
                                {
                                    new SqlParameter("@Title", news.Title),
                                    new SqlParameter("@Text", news.Text),
                                    new SqlParameter("@Author", news.Author),
                                    new SqlParameter("@PublishDate", news.PublishDate),
                                    new SqlParameter("@UpdateDate", news.UpdateDate),
                                    new SqlParameter("@Category", news.Category)
                                };
            return Convert.ToInt32(ExecuteScalar("AddNews", parametrs));
        }

        public void SetRating(int rating, int newsId, string login)
        {
            var parameters = new List<SqlParameter>
                                 {
                                     new SqlParameter("@Rating", rating),
                                     new SqlParameter("@NewsID", newsId),
                                     new SqlParameter("@Login", login)
                                 };
            ExecuteNonQuery("SetRating", parameters);
        }

        public decimal GetRating(int id)
        {
            var dataTable = ExecuteDataTable("GetRating", new List<SqlParameter> {new SqlParameter("@NewsID", id)});
        
            if(dataTable.Rows.Count > 0)
            {
                return Convert.ToDecimal(dataTable.Rows[0]["Rating"]);
            }
            return 0;
        }

        public bool VoteOrNot(string login, int newsId)
        {
            if(ExecuteScalar("VoteOrNot", new List<SqlParameter>
                                              {
                                                  new SqlParameter("@Login", login),
                                                  new SqlParameter("@NewsID" ,newsId)
                                              }
              ) != null) return true;
               
           return false;
        }

        public void DeleteTagsBind(int newsId)
        {
            ExecuteNonQuery("DeleteTagsBind", new List<SqlParameter>
                                                  {
                                                      new SqlParameter("@NewsID",newsId)
                                                  });
        }
    }
}